<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

/*
 * do basic housekeeping tasks
 */

require_once('admin.php');

$title = 'Preformatter';
$this_file = 'lightpress/wp-plugin/preformatter.php';
$parent_file = 'lightpress/wp-plugin/main.php';

$submenu[$this_file][] = array('LightPress', 'manage_options', 'lightpress/wp-plugin/preformatter.php', 'LightPress');

/*
 * instantiate LightPressOptions and the Template object
 */

if (!isset($lp_opts)) {
    require_once 'LightPressOptions.php';
    $lp_opts = new LightPressOptions();
} else if (!is_object($lp_opts) || !is_subclass_of($lp_opts, 'LightPressOptions')) {
    $lp_opts = new LightPressOptions();
}

require_once implode(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'classes', 'Template.php'));
$tpl =& new Template(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'templates-'.__('en_US','lp'))));

$tpl->setFile('main', 'preformatter.html');

$opts = array('use_preformatter', 'preformatter_method');

$tpl_vars = array();

if (isset($_POST['activate'])) {
    foreach ($opts as $opt) {
        if (isset($_POST["lp_opt_$opt"]))
            $lp_opts->updateOption("lp_opt_$opt", $_POST["lp_opt_$opt"]);
    }
}

// set options for select lists
$tpl_vars['lp_opt_use_preformatter_1'] = '';
if ($lp_opts->getOption('use_preformatter') == '1')
    $tpl_vars['lp_opt_use_preformatter_1'] = ' selected';
$tpl_vars['lp_opt_preformatter_method_lp'] = '';
if ($lp_opts->getOption('preformatter_method') == 'lp')
    $tpl_vars['lp_opt_preformatter_method_lp'] = ' selected';

$include_all = isset($_POST['include_all']) ? (int)$_POST['include_all'] : 0;
$tpl_vars['include_all'] = $include_all == 1 ? ' selected' : '';

$batch_size = isset($_POST['batch_size']) ? (int)$_POST['batch_size'] : 0;
foreach (array(10, 50, 100) as $k) {
    $tpl_vars["batch_size_$k"] = $k == $batch_size ? ' selected' : '';
}

$batch_start = isset($_POST['batch_start']) ? (int)$_POST['batch_start'] : 0;

$batch_method = isset($_POST['batch_method']) ? $_POST['batch_method'] : $lp_opts->getOption('preformatter_method');
$tpl_vars['batch_method'] = $batch_method == 'lp' ? ' selected' : '';

if (isset($_POST['convert'])) {
    require_once $lp_opts->basedir . '/classes/LightpressPreformatter.php';
    $p =& new LightpressPreformatter($batch_method);
    $q = "select ID, post_content from $wpdb->posts where post_status='publish'";
    if (!$include_all)
        $q .= " and (post_content_filtered = '' or post_content_filtered is null)";
    $q .= " order by id";
    if ($batch_size) {
        $q .= " limit $batch_start, $batch_size";
        $batch_start += $batch_size;
    }
    $content = $wpdb->get_results($q, ARRAY_N);
    if (is_array($content)) {
        foreach ($content as $row) {
            $p->preformat($row[0], $row[1]);
        }
    }
} else if (isset($_POST['clear'])) {
    $wpdb->query("update $wpdb->posts set post_content_filtered = ''");
}

$tpl_vars["batch_start"] = $batch_start;
$tpl->setVar($tpl_vars);

$out =& $tpl->parse('MAIN', 'main');
echo $out;

?>
